import huck
import huck.api.user
import google.appengine.api.users

class User(huck.api.user.Abstract):

  def __init__(self, handler, type):
    super(User, self).__init__(handler, type)
    self.__setup = False

  def __setup(self):
    if not self._setup():
      self.__user = google.appengine.api.users.get_current_user()
      self.__setup = True
    if not self.__user:
      raise huck.UserError('user not authenticated')

  def id(self):
    self.__setup()
    return self.__user.user_id()

  def email(self):
    self.__setup()
    return self.__user.email()

  def name(self):
    self.__setup()
    return self.__user.nickname()

  @staticmethod
  def login_url(url='/'):
    return google.appengine.api.users.create_login_url(url)

  @staticmethod
  def logout_url(url='/'):
    return google.appengine.api.users.create_logout_url(url)

class Create(object):

  def __init__(self, type='default'):
    self.type = type

  def setup(self, application):
    huck.api.user.set(User, self.type)
    return application
